define([
    'modules/ApiClient',
	'modules/config',
    "jquery",
    'jquery.cookie'
], function (ApiClient,config, $){
	serverAddress = "ws://127.0.0.1:4004/ytws";
    
    nicknameTextId = "nicknameText";
    nicknameButtonId = "nicknameButton";
    nicknameModalId = "setNickname";
    messageTextId = "messageText";

    var chatThreadId = "mainThread";
	var pg=document.getElementById('pg');
    
    var namespaceName,
        ClassName,
        ws,
        message = {
            content: "",
            namespaceName: "",
            ClassName:""
        };
    
    /**
    * Set nickname. Will be used in following communication.
    */
    var setNickname = function () {
        namespaceName = document.getElementById(this.nicknameTextId).value;
        if (namespaceName !== undefined && namespaceName !== "") {
            $('#' + this.nicknameModalId).modal('hide');
            document.getElementById(this.messageTextId).focus();
            document.getElementById(this.nicknameTextId).value = "";
        }
        
    };
    
    /**
    * Send message to server. Username is appended automatically.
    *
    * text: Message to send
    */
    var sendMessage = function (text) {
		alert(text);
        if (ws !== undefined && text !== undefined && text !== "") {
            var msg = message;
            msg.content = "{'token':'"+text+"','fid':'694'}";
            msg.namespaceName = "RSM.WS";
            msg.ClassName = "WsCompressImg";
            namespaceName = "RSM.WS";
            ClassName = "WsCompressImg";
            msg = JSON.stringify(msg);
            
            document.getElementById(messageTextId).value = '';
            
            ws.send(msg);
        } else {
            console.log("No websocket connection initialized or message is empty.");
        }
    };
    
    /**
    * Initialize WebSocket stream.
    */
    var init = function () {
		alert(serverAddress);
        ws = new WebSocket(serverAddress);
        ws.onmessage = onMessage;
        ws.onclose = onClose;
        ws.onopen = onOpen;
    };
    
    var appendMessageToThread = function (message, namespaceName, ClassName) {
        var str =
            '<p>'
            + '    <b>' + namespaceName + '.' + ClassName + ':</b> ' + message
            + '</p>',
            div = document.getElementById(chatThreadId);
        div.insertAdjacentHTML("beforeend", str);
        window.scrollTo(0, document.body.scrollHeight);
    },
    
        onMessage = function(event) {
        //var msg = JSON.parse(event.data);
			var message = JSON.parse(event.data);
			//alert(message.Messagetype);
			if(message.Messagetype == 'Process'){
				if(message.DV<100) pg.value = message.DV;
				else pg.value=100;
			}else if(message.Messagetype == 'Result'){
				appendMessageToThread(message.DownloadUri, namespaceName, ClassName);
			}else{
				console.log("Message content: " + event.data + ", namespaceName: " + namespaceName + ", ClassName: " + ClassName);
				appendMessageToThread(event.data, namespaceName, ClassName);
			}
        },
    
        onOpen = function(event) {
			console.log("Connection established.");
			appendMessageToThread("<span class=\"text-danger\">Connection is open.</span>", "System ","message");
		},
    
        onClose = function () {
            appendMessageToThread("<span class=\"text-danger\">Connection is closed.</span>", "System ","message");
            console.log("WS connection closed.");
            ws = undefined;
        };
		
	return {
        init:init,
		sendMessage:sendMessage
    }
	
});